﻿using DemoTfsUtility;
using Kinban.Contracts;
using Kinban.TfsUtility;
using Kinban.Users;
using Kinect.KinectMouseControl;
using Kinban.Properties;

namespace Kinban.ViewModels
{
    public class MainViewModel : IInitializable
    {
        public MainViewModel()
        {
            KinectViewModel = new KinectMouseViewModel();
             
            var userGateway = new DemoUserGateway();

            ITfsGateway tfsGateway;

            if (!string.IsNullOrEmpty(Settings.Default.TfsProjectPortalUrl))
            {
                tfsGateway = new TfsAccess(Settings.Default.TfsProjectPortalUrl);
            }
            else
            {
                tfsGateway = new DemoTfsAccess();
            }
            
            TfsViewModel = new TfsViewModel(tfsGateway, userGateway);
        }

        public KinectMouseViewModel KinectViewModel { get; set; }
        public TfsViewModel TfsViewModel { get; set; }

        #region IInitializable Members

        public void Initialize()
        {
            //.SubscribeOnDispatcher()
            KinectViewModel.Initialize();

            //var syncContext = new System.Reactive.Concurrency.SynchronizationContextScheduler(DispatcherSynchronizationContext.Current);

            //KinectViewModel.VideoImage.SubscribeOnDispatcher().Subscribe(x => image = x);

            KinectViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(KinectViewModel_PropertyChanged);
        }

        void KinectViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "VideoImage")
            {
                TfsViewModel.VideoImage = KinectViewModel.VideoImage;
                return;
            }

            if (e.PropertyName == "DepthImage")
            {
                TfsViewModel.DepthImage = KinectViewModel.DepthImage;
                return;
            }
        }

        #endregion

        private object image;

        #region IDisposable Members

        public void Dispose()
        {
            KinectViewModel.Dispose();
        }

        #endregion
    }
}
